package com.yunji.base.business;

import com.google.common.collect.Lists;
import com.yunji.base.business.common.SiteAuditCommonBusiness;
import com.yunji.base.cache.BaseCache;
import com.yunji.base.domain.SiteAuditDO;
import com.yunji.base.domain.SiteScaleDO;
import com.yunji.base.dto.SiteAuditDTO;
import com.yunji.base.engine.validate.SiteValidate;
import com.yunji.base.params.SiteAuditParam;
import com.yunji.base.dto.SiteDTO;
import com.yunji.base.enums.AuditStatus;
import com.yunji.base.service.SiteAuditService;
import com.yunji.base.service.SiteScaleService;
import com.yunji.base.vo.SiteAuditVO;
import com.yunji.common.enums.business.AuditTypes;
import com.yunji.common.enums.common.ErrorEnum;
import com.yunji.common.utils.TimeUtils;
import com.yunji.common.utils.bean.BeanUtils;
import com.yunji.system.context.SystemContext;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.List;

@Component
public class SiteAuditBusiness {

    @Resource
    private SiteAuditService siteAuditService;

    @Resource
    private SiteScaleService siteScaleService;

    @Resource
    private SiteAuditCommonBusiness siteAuditCommonBusiness;

    public List<SiteAuditVO> search(SiteAuditParam param) {
        return siteAuditService.search(param);
    }

    public SiteAuditVO getInfo(Long auditId) {
        return siteAuditService.selectVO(auditId);
    }

    public void audit(Long auditId, Integer auditStatus, String reason) {
        SiteAuditDO update = new SiteAuditDO();
        update.setAuditId(auditId);
        update.setAuditStatus(auditStatus);

        SiteAuditDO siteAudit = siteAuditService.select(auditId);
        Assert.isTrue(BeanUtils.isNotNull(siteAudit), ErrorEnum.BUSINESS_612003.getText());
        Assert.isTrue(AuditStatus.WAIT.getCode().equals(siteAudit.getAuditStatus()), ErrorEnum.BUSINESS_612004.getText());

        if (AuditStatus.REJECT.getCode().equals(auditStatus)) {
            Assert.isTrue(BeanUtils.isNotNull(reason), ErrorEnum.BUSINESS_812006.getText());
            update.setReason(reason);
        } else {
            // 是账号审核还是修改分润审核
            Integer auditType = siteAudit.getAuditType();
            SiteDTO dto = new SiteDTO();
            BeanUtils.clone(siteAudit, dto);
            List<SiteScaleDO> list = siteScaleService.selectByAuditId(auditId);
            dto.setSiteScaleList(list);
            if (AuditTypes.ACCOUNT.getCode().equals(auditType)) {
                siteAuditCommonBusiness.handleAgentSupport(dto);
            }
            if (AuditTypes.SCALE.getCode().equals(auditType)) {
                dto.setId(siteAudit.getSiteId());
                siteAuditCommonBusiness.updateCooperateWay(dto);
            }
        }
        update.setAuditBelongId(SystemContext.getBelongId());
        update.setAuditBelongName(BaseCache.getBelongName(SystemContext.getBelongId()));
        update.setAuditTime(TimeUtils.getNowDate());
        siteAuditService.update(update);
    }

    public boolean cancel(Long auditId) {
        return siteAuditService.modifyStatus(auditId, AuditStatus.CANCEL.getCode(), Lists.newArrayList(AuditStatus.WAIT.getCode()));
    }

    @Transactional
    public void commit(SiteAuditDTO dto) {
        SiteDTO siteDTO = new SiteDTO();
        BeanUtils.clone(dto, siteDTO);
        siteDTO.setId(dto.getSiteId());
        Integer auditType = dto.getAuditType();
        if (AuditTypes.ACCOUNT.getCode().equals(auditType)) {
            SiteValidate.validatePhone(dto.getSiteTel());
            SiteValidate.validateSiteParam(siteDTO);
            siteAuditCommonBusiness.checkSite(siteDTO);
            siteAuditCommonBusiness.handleCreateSite(siteDTO);
        }else {
            SiteValidate.validateSiteParam(siteDTO);
            siteAuditCommonBusiness.checkSite(siteDTO);
            siteAuditCommonBusiness.handleUpdateAuditSite(siteDTO);
        }
    }
}
